#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import cv2
from PIL import Image
from glob import glob


class ImgReader:

    def __init__(self):
        pass

    # 根据输入的文件夹绝对路径，将该文件夹下的所有指定suffix的文件读取存入一个list,该list的第一个元素是该文件夹的名字
    def readAllImg(self, path, *suffix):
        resultArray = []
        try:
            files = os.listdir(path)
            fileName = os.path.basename(path)
            resultArray.append(fileName)
            for file in files:
                if self.endwith(file, suffix):
                    document = os.path.join(path, file)
                    img = cv2.imread(document)
                    resultArray.append(img)
        except IOError as ex:
            print(ex)
        return resultArray

    # 输入一个字符串一个标签，对这个字符串的后续和标签进行匹配
    def endwith(self, s, *endstring):
        resultArray = map(s.endswith, endstring)
        if resultArray:
            return True
        else:
            return False


if __name__ == '__main__':
    instance = ImgReader()
    result = instance.readAllImg("D:/opt/images", '.jpg', '.png', '.gif')
    print(result[0])
    for img in result:

        cv2.imshow("Image", img)
        if cv2.waitKey(0) & 0xFF == ord('q'):
            continue

    cv2.waitKey(0)
    cv2.destroyAllWindows()